/**
 * 
 */
package org.swing.utility.common.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
 * @author lqnhu
 *
 */
public final class StackTraceUtil {
	public static String getStackTrace(Throwable aThrowable) {
		final Writer result = new StringWriter();
		final PrintWriter printWriter = new PrintWriter(result);
		aThrowable.printStackTrace(printWriter);
		return result.toString();
	}

	/**
	 * Defines a custom format for the stack trace as String.
	 */
	public static String toStackTraceString(String firstLine,
			StackTraceElement[] stack) {
		// add the class name and any message passed to constructor
		final StringBuilder result = new StringBuilder(firstLine);
		// result.append(aThrowable.toString());
		final String NEW_LINE = System.getProperty("line.separator");
		result.append(NEW_LINE);
		// add each element of the stack trace
		for (StackTraceElement element : stack) {
			result.append(" at ");
			result.append(element);
			result.append(NEW_LINE);
		}
		return result.toString();
	}
}
